1 // Debugging deque implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_DEQUE
31 #define _GLIBCXX_DEBUG_DEQUE 1
34 #include <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
37 namespace std _GLIBCXX_VISIBILITY(default)
41 /// Class std::deque with safety/checking/debug instrumentation.
42 template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
44 : public _GLIBCXX_STD_C::deque<_Tp, _Allocator>,
45 public __gnu_debug::_Safe_sequence<deque<_Tp, _Allocator> >
47 typedef _GLIBCXX_STD_C::deque<_Tp, _Allocator> _Base;
48 typedef __gnu_debug::_Safe_sequence<deque> _Safe_base;
50 typedef typename _Base::const_iterator _Base_const_iterator;
51 typedef typename _Base::iterator _Base_iterator;
52 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
54 typedef typename _Base::reference reference;
55 typedef typename _Base::const_reference const_reference;
57 typedef __gnu_debug::_Safe_iterator<_Base_iterator,deque>
59 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,deque>
62 typedef typename _Base::size_type size_type;
63 typedef typename _Base::difference_type difference_type;
65 typedef _Tp value_type;
66 typedef _Allocator allocator_type;
67 typedef typename _Base::pointer pointer;
68 typedef typename _Base::const_pointer const_pointer;
69 typedef std::reverse_iterator<iterator> reverse_iterator;
70 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
72 // 23.2.1.1 construct/copy/destroy:
74 deque(const _Allocator& __a = _Allocator())
77 #ifdef __GXX_EXPERIMENTAL_CXX0X__
82 deque(size_type __n, const _Tp& __value,
83 const _Allocator& __a = _Allocator())
84 : _Base(__n, __value, __a) { }
87 deque(size_type __n, const _Tp& __value = _Tp(),
88 const _Allocator& __a = _Allocator())
89 : _Base(__n, __value, __a) { }
92 template<class _InputIterator>
93 deque(_InputIterator __first, _InputIterator __last,
94 const _Allocator& __a = _Allocator())
95 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
97 __gnu_debug::__base(__last), __a)
100 deque(const deque& __x)
101 : _Base(__x), _Safe_base() { }
103 deque(const _Base& __x)
104 : _Base(__x), _Safe_base() { }
106 #ifdef __GXX_EXPERIMENTAL_CXX0X__
108 : _Base(std::move(__x)), _Safe_base()
109 { this->_M_swap(__x); }
111 deque(initializer_list<value_type> __l,
112 const allocator_type& __a = allocator_type())
113 : _Base(__l, __a), _Safe_base() { }
119 operator=(const deque& __x)
121 *static_cast<_Base*>(this) = __x;
122 this->_M_invalidate_all();
126 #ifdef __GXX_EXPERIMENTAL_CXX0X__
128 operator=(deque&& __x)
138 operator=(initializer_list<value_type> __l)
140 *static_cast<_Base*>(this) = __l;
141 this->_M_invalidate_all();
146 template<class _InputIterator>
148 assign(_InputIterator __first, _InputIterator __last)
150 __glibcxx_check_valid_range(__first, __last);
151 _Base::assign(__gnu_debug::__base(__first),
152 __gnu_debug::__base(__last));
153 this->_M_invalidate_all();
157 assign(size_type __n, const _Tp& __t)
159 _Base::assign(__n, __t);
160 this->_M_invalidate_all();
163 #ifdef __GXX_EXPERIMENTAL_CXX0X__
165 assign(initializer_list<value_type> __l)
168 this->_M_invalidate_all();
172 using _Base::get_allocator;
177 { return iterator(_Base::begin(), this); }
181 { return const_iterator(_Base::begin(), this); }
185 { return iterator(_Base::end(), this); }
189 { return const_iterator(_Base::end(), this); }
193 { return reverse_iterator(end()); }
195 const_reverse_iterator
197 { return const_reverse_iterator(end()); }
201 { return reverse_iterator(begin()); }
203 const_reverse_iterator
205 { return const_reverse_iterator(begin()); }
207 #ifdef __GXX_EXPERIMENTAL_CXX0X__
210 { return const_iterator(_Base::begin(), this); }
214 { return const_iterator(_Base::end(), this); }
216 const_reverse_iterator
218 { return const_reverse_iterator(end()); }
220 const_reverse_iterator
222 { return const_reverse_iterator(begin()); }
227 _M_invalidate_after_nth(difference_type __n)
229 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
230 this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
234 // 23.2.1.2 capacity:
236 using _Base::max_size;
238 #ifdef __GXX_EXPERIMENTAL_CXX0X__
240 resize(size_type __sz)
242 bool __invalidate_all = __sz > this->size();
243 if (__sz < this->size())
244 this->_M_invalidate_after_nth(__sz);
248 if (__invalidate_all)
249 this->_M_invalidate_all();
253 resize(size_type __sz, const _Tp& __c)
255 bool __invalidate_all = __sz > this->size();
256 if (__sz < this->size())
257 this->_M_invalidate_after_nth(__sz);
259 _Base::resize(__sz, __c);
261 if (__invalidate_all)
262 this->_M_invalidate_all();
266 resize(size_type __sz, _Tp __c = _Tp())
268 bool __invalidate_all = __sz > this->size();
269 if (__sz < this->size())
270 this->_M_invalidate_after_nth(__sz);
272 _Base::resize(__sz, __c);
274 if (__invalidate_all)
275 this->_M_invalidate_all();
279 #ifdef __GXX_EXPERIMENTAL_CXX0X__
280 using _Base::shrink_to_fit;
287 operator[](size_type __n)
289 __glibcxx_check_subscript(__n);
290 return _M_base()[__n];
294 operator[](size_type __n) const
296 __glibcxx_check_subscript(__n);
297 return _M_base()[__n];
305 __glibcxx_check_nonempty();
306 return _Base::front();
312 __glibcxx_check_nonempty();
313 return _Base::front();
319 __glibcxx_check_nonempty();
320 return _Base::back();
326 __glibcxx_check_nonempty();
327 return _Base::back();
330 // 23.2.1.3 modifiers:
332 push_front(const _Tp& __x)
334 _Base::push_front(__x);
335 this->_M_invalidate_all();
339 push_back(const _Tp& __x)
341 _Base::push_back(__x);
342 this->_M_invalidate_all();
345 #ifdef __GXX_EXPERIMENTAL_CXX0X__
347 push_front(_Tp&& __x)
348 { emplace_front(std::move(__x)); }
352 { emplace_back(std::move(__x)); }
354 template<typename... _Args>
356 emplace_front(_Args&&... __args)
358 _Base::emplace_front(std::forward<_Args>(__args)...);
359 this->_M_invalidate_all();
362 template<typename... _Args>
364 emplace_back(_Args&&... __args)
366 _Base::emplace_back(std::forward<_Args>(__args)...);
367 this->_M_invalidate_all();
370 template<typename... _Args>
372 emplace(iterator __position, _Args&&... __args)
374 __glibcxx_check_insert(__position);
375 _Base_iterator __res = _Base::emplace(__position.base(),
376 std::forward<_Args>(__args)...);
377 this->_M_invalidate_all();
378 return iterator(__res, this);
383 insert(iterator __position, const _Tp& __x)
385 __glibcxx_check_insert(__position);
386 _Base_iterator __res = _Base::insert(__position.base(), __x);
387 this->_M_invalidate_all();
388 return iterator(__res, this);
391 #ifdef __GXX_EXPERIMENTAL_CXX0X__
393 insert(iterator __position, _Tp&& __x)
394 { return emplace(__position, std::move(__x)); }
397 insert(iterator __p, initializer_list<value_type> __l)
399 _Base::insert(__p, __l);
400 this->_M_invalidate_all();
405 insert(iterator __position, size_type __n, const _Tp& __x)
407 __glibcxx_check_insert(__position);
408 _Base::insert(__position.base(), __n, __x);
409 this->_M_invalidate_all();
412 template<class _InputIterator>
414 insert(iterator __position,
415 _InputIterator __first, _InputIterator __last)
417 __glibcxx_check_insert_range(__position, __first, __last);
418 _Base::insert(__position.base(), __gnu_debug::__base(__first),
419 __gnu_debug::__base(__last));
420 this->_M_invalidate_all();
426 __glibcxx_check_nonempty();
427 this->_M_invalidate_if(_Equal(_Base::begin()));
434 __glibcxx_check_nonempty();
435 this->_M_invalidate_if(_Equal(--_Base::end()));
440 erase(iterator __position)
442 __glibcxx_check_erase(__position);
443 _Base_iterator __victim = __position.base();
444 if (__victim == _Base::begin() || __victim == _Base::end()-1)
446 this->_M_invalidate_if(_Equal(__victim));
447 return iterator(_Base::erase(__victim), this);
451 _Base_iterator __res = _Base::erase(__victim);
452 this->_M_invalidate_all();
453 return iterator(__res, this);
458 erase(iterator __first, iterator __last)
460 // _GLIBCXX_RESOLVE_LIB_DEFECTS
461 // 151. can't currently clear() empty container
462 __glibcxx_check_erase_range(__first, __last);
464 if (__first.base() == __last.base())
466 else if (__first.base() == _Base::begin()
467 || __last.base() == _Base::end())
469 this->_M_detach_singular();
470 for (_Base_iterator __position = __first.base();
471 __position != __last.base(); ++__position)
473 this->_M_invalidate_if(_Equal(__position));
477 return iterator(_Base::erase(__first.base(), __last.base()),
482 this->_M_revalidate_singular();
483 __throw_exception_again;
488 _Base_iterator __res = _Base::erase(__first.base(),
490 this->_M_invalidate_all();
491 return iterator(__res, this);
506 this->_M_invalidate_all();
510 _M_base() { return *this; }
513 _M_base() const { return *this; }
516 template<typename _Tp, typename _Alloc>
518 operator==(const deque<_Tp, _Alloc>& __lhs,
519 const deque<_Tp, _Alloc>& __rhs)
520 { return __lhs._M_base() == __rhs._M_base(); }
522 template<typename _Tp, typename _Alloc>
524 operator!=(const deque<_Tp, _Alloc>& __lhs,
525 const deque<_Tp, _Alloc>& __rhs)
526 { return __lhs._M_base() != __rhs._M_base(); }
528 template<typename _Tp, typename _Alloc>
530 operator<(const deque<_Tp, _Alloc>& __lhs,
531 const deque<_Tp, _Alloc>& __rhs)
532 { return __lhs._M_base() < __rhs._M_base(); }
534 template<typename _Tp, typename _Alloc>
536 operator<=(const deque<_Tp, _Alloc>& __lhs,
537 const deque<_Tp, _Alloc>& __rhs)
538 { return __lhs._M_base() <= __rhs._M_base(); }
540 template<typename _Tp, typename _Alloc>
542 operator>=(const deque<_Tp, _Alloc>& __lhs,
543 const deque<_Tp, _Alloc>& __rhs)
544 { return __lhs._M_base() >= __rhs._M_base(); }
546 template<typename _Tp, typename _Alloc>
548 operator>(const deque<_Tp, _Alloc>& __lhs,
549 const deque<_Tp, _Alloc>& __rhs)
550 { return __lhs._M_base() > __rhs._M_base(); }
552 template<typename _Tp, typename _Alloc>
554 swap(deque<_Tp, _Alloc>& __lhs, deque<_Tp, _Alloc>& __rhs)
555 { __lhs.swap(__rhs); }
557 } // namespace __debug